<?php
	
	function dbconnect() 
	{ 
		include('config.php'); 
		static $connect = null; 
		if ($connect === null) 
			{
				 $connect =  mysql_connect($host, $user, $passwd) or die(mysql_error());
				 mysql_query("SET character_set_results=utf8", $connect);
				 mysql_select_db($database); 
			} 
		return $connect; 
	} 
	
	
	
	function getid_auto_increment()
	{
		
		$r= mysql_query("SHOW TABLE STATUS LIKE 'Sach'",dbconnect());
		$row = mysql_fetch_array($r); 
        $n = $row['Auto_increment']; 
        return $n;
		
		} 
	function getid_auto_increment_dexuat()
	{
		
		$r= mysql_query("SHOW TABLE STATUS LIKE 'De_xuat'",dbconnect());
		$row = mysql_fetch_array($r); 
        $n = $row['Auto_increment']; 
        return $n;
		
		}
	function getbyall_tacgia()
	{ 
		$allnxb_req = mysql_query("SELECT * FROM Tac_gia" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	

	function Menu($parent = 0, $space = " ", $trees = array())

			{
			if(!$trees) { $trees = array();}
			$sql = "SELECT * FROM Chu_de WHERE Parent = $parent";
			$query = mysql_query($sql, dbconnect());
			
			while($rs = mysql_fetch_assoc($query))
			
					{
					$trees[] = array( 'ID' => $rs['ID'],
										
										'Ten_chu_de'=>$space." ".$rs['Ten_chu_de'], 
										'Parent' => $rs['Parent'],);
			
					$trees = Menu($rs['ID'], $space."--", $trees); 
					
				    }
			return $trees;
			
			}
			
   function getbyall_nxb()
	{ 
		$allnxb_req = mysql_query("SELECT * FROM Nha_xuat_ban" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
				
   
	function getbyall_tts()
	{ 
		$allnxb_req = mysql_query("SELECT * FROM Tinh_trang_sach" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	function getbyid_thamgia($id)
	{ 
		$allnxb_req = mysql_query("SELECT * FROM Tham_gia WHERE ID_Sach = N'".$id."'" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	function getbyall_gg()
	{ 
		$allnxb_req = mysql_query("SELECT * FROM Giam_gia" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	
	
	function insert_sach($post,$imgname) 
	{ 
	 
		$query= mysql_query("INSERT INTO Sach SET ID_Nha_xuat_ban=N'".$post['ID_Nha_xuat_ban']."', ID_Tinh_trang_sach=N'".$post['ID_Tinh_trang_sach']."', ID_Chu_de=N'".$post['ID_Chu_de']."', ID_Giam_gia=N'".$post['ID_Giam_gia']."',Ten_sach=N'".$post['Ten_sach']."',  Gioi_thieu=N'".$post['Gioi_thieu']."',  Gia_bia=N'".$post['Gia_bia']."', Nam_xuat_ban=N'".$post['Nam_xuat_ban']."', So_trang=N'".$post['So_trang']."', Dang_bia=N'".$post['Dang_bia']."', Kich_thuoc=N'".$post['Kich_thuoc']."', Khoi_luong=N'".$post['Khoi_luong']."', Hinh_minh_hoa=N'".$imgname."', Luot_xem=N'".$post['Luot_xem']."',So_luong=N'".$post['So_luong']."', Trang_thai='1', Thoi_gian=now() ", dbconnect());  
		
	}
	function update_sach($post,$imgname) 
	{ 
	 
		$query= mysql_query("UPDATE Sach SET 
		ID_Nha_xuat_ban=N'".$post['ID_Nha_xuat_ban']."', 
		ID_Tinh_trang_sach=N'".$post['ID_Tinh_trang_sach']."', 
		ID_Chu_de=N'".$post['ID_Chu_de']."', 
		ID_Giam_gia=N'".$post['ID_Giam_gia']."',
		Ten_sach=N'".$post['Ten_sach']."',  
		Gioi_thieu=N'".$post['Gioi_thieu']."',  
		Gia_bia=N'".$post['Gia_bia']."', 
		Nam_xuat_ban=N'".$post['Nam_xuat_ban']."', 
		So_trang=N'".$post['So_trang']."', 
		Dang_bia=N'".$post['Dang_bia']."', 
		Kich_thuoc=N'".$post['Kich_thuoc']."', 
		Khoi_luong=N'".$post['Khoi_luong']."', 
		Hinh_minh_hoa=N'".$imgname."', 
		Luot_xem=N'".$post['Luot_xem']."',
		So_luong=N'".$post['So_luong']."', 
		Trang_thai='1', 
		Thoi_gian=now() 
		where ID= N'".$post['ID']."'", dbconnect());  
		
	}
	function update_sach2($post) 
	{ 
	 
		$query= mysql_query("UPDATE Sach SET 
		ID_Nha_xuat_ban=N'".$post['ID_Nha_xuat_ban']."', 
		ID_Tinh_trang_sach=N'".$post['ID_Tinh_trang_sach']."', 
		ID_Chu_de=N'".$post['ID_Chu_de']."', 
		ID_Giam_gia=N'".$post['ID_Giam_gia']."',
		Ten_sach=N'".$post['Ten_sach']."',  
		Gioi_thieu=N'".$post['Gioi_thieu']."',  
		Gia_bia=N'".$post['Gia_bia']."', 
		Nam_xuat_ban=N'".$post['Nam_xuat_ban']."', 
		So_trang=N'".$post['So_trang']."', 
		Dang_bia=N'".$post['Dang_bia']."', 
		Kich_thuoc=N'".$post['Kich_thuoc']."', 
		Khoi_luong=N'".$post['Khoi_luong']."', 
		
		Luot_xem=N'".$post['Luot_xem']."',
		So_luong=N'".$post['So_luong']."', 
		Trang_thai='1', 
		Thoi_gian=now()
		where ID= N'".$post['ID']."'", dbconnect());  
		
	}
	function insert_sach2($post,$imgname) 
	{ 
	 
		$query= mysql_query("INSERT INTO Sach SET ID_Nha_xuat_ban=N'".$post['ID_Nha_xuat_ban']."', ID_Tinh_trang_sach=N'".$post['ID_Tinh_trang_sach']."', ID_Chu_de=N'".$post['ID_Chu_de']."', ID_Giam_gia=N'".$post['ID_Giam_gia']."',Ten_sach=N'".$post['Ten_sach']."',  Gioi_thieu=N'".$post['Gioi_thieu']."',  Gia_bia=N'".$post['Gia_bia']."', Nam_xuat_ban=N'".$post['Nam_xuat_ban']."', So_trang=N'".$post['So_trang']."', Dang_bia=N'".$post['Dang_bia']."', Kich_thuoc=N'".$post['Kich_thuoc']."', Khoi_luong=N'".$post['Khoi_luong']."', Hinh_minh_hoa=N'".$imgname."', Luot_xem=N'".$post['Luot_xem']."',So_luong=N'".$post['So_luong']."', Trang_thai='2' ", dbconnect());  
		
	}
	function insert_thamgia($tg, $sach)
	{
		$query = mysql_query("INSERT INTO Tham_gia SET ID_Tac_gia =N'".$tg."',ID_Sach =N'".$sach."' ");
		}
	function insert_dexuat($dx)
	{
		$query= mysql_query("INSERT INTO De_xuat SET ID_Sach=N'".$dx['ID_Sach']."',  Thoi_gian=now() ", dbconnect());  
		}
	function insert_giaban($gb)
	{
		$query = mysql_query("INSERT INTO gia_ban SET ID_Sach =N'".$gb['ID']."',Gia_ban =N'".$gb['Gia_ban']."', Thoi_gian =N'".$gb['Thoi_gian_ban']."' ");
		}
	function getbyid_sach($id)
	{
		$result= mysql_query("SELECT * FROM Sach WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_tacgia($id)
	{
		$result= mysql_query("SELECT * FROM Tac_gia WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_nxb($id)
	{
		$result= mysql_query("SELECT * FROM nha_xuat_ban WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_cd($id)
	{
		$result= mysql_query("SELECT * FROM chu_de WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_tg($id)
	{
		$allnxb_req= mysql_query("SELECT tham_gia.*, Ten_tac_gia FROM Tham_gia 
								INNER JOIN Tac_gia
                                ON Tham_gia.ID_Tac_gia=Tac_gia.ID
                                WHERE Tham_gia.ID_Sach=N'".$id."'",dbconnect());
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	}
	
	function getbyid_gb($id)
	{
		$result= mysql_query("SELECT * FROM gia_ban WHERE (ID_Sach=N'".$id."' and Thoi_gian<now()) 
													order by Thoi_gian DESC limit 1", dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_gg($id)
	{
		$result= mysql_query("SELECT * FROM Giam_gia WHERE ID=N'".$id."' ", dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_gianhap($id)
	{
		$result= mysql_query("SELECT * FROM Chi_tiet_nhap WHERE (ID_Sach='".$id."' ) order by ID DESC limit 1",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_giaban($id)
	{
		$result= mysql_query("SELECT * FROM gia_ban WHERE (ID_Sach='".$id."' and Thoi_gian<now()) order by Thoi_gian DESC limit 1",dbconnect());
		return mysql_fetch_array($result); 
	}
	function getbyid_hoadonnhap($id)
	{
		$result= mysql_query("SELECT * FROM Hoa_don_nhap WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
	function insert_binhluansach($bv,$id) 
			{ 
	 
		$result= mysql_query("INSERT INTO Binh_luan_sach
		SET 
		Ten=N'".$bv['Ten']."', 
		Email=N'".$bv['Email']."',  
		Noi_dung=N'".$bv['Noi_dung']."',  
		Thoi_gian=now(), 
		Trang_thai='2',
		ID_Sach=N'".$id."' ", dbconnect());  
		
		if ($result)
			{
		echo "<script> alert('Bình luận đã lưu, chúng tôi sẽ xem xét trước khi nó hiển thị công cộng!')</script>";
		mysql_close();
			}
			else
			{
			echo "<script> alert('Quá trình thêm dữ liệu bị lỗi!')</script>" . mysql_error();
			}
		
			}
	
	function getbyid_binhluan($id)
	{ 
		$allnxb_req = mysql_query("SELECT *
		FROM `Binh_luan_sach` 
		where (Trang_thai=1 and ID_Sach=N'".$id."') order by ID desc " ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
	function getbyall_sach()
	{ 
		$allnxb_req = mysql_query("SELECT * FROM sach" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
?>